/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

/**
 *
 * @author Le Thai
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import util.ClientSetting;

public class DbAccess {

    Connection conn;
    private String hostname;
    private String portNumber;
    private String dbname;
    private String username;
    private String password;
    ResultSet res;
    Statement stm;
    String url;

    public DbAccess() {
        conn = null;
        hostname = "";
        portNumber = "";
        dbname = "";
        username = "";
        password = "";
        url = "";
    }

    public boolean getDefaultConnection() {
        //Missing method get connection setting, use manual setting
        hostname = ClientSetting.getHostName();
        portNumber =ClientSetting.getPort();
        dbname = ClientSetting.getDbname();
        username = ClientSetting.getUserName();
        password = ClientSetting.getPassword();

        //prepare new connection with MSSQL server's driver
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            System.out.println("Class not found!");
            return false;
        }

        //get connecttion
        url = "jdbc:sqlserver://" + hostname;
        if (!portNumber.equals("")) {
            url += ":" + portNumber;
        }
        url += ";databaseName=" + dbname;

        try {
            conn = DriverManager.getConnection(url, username, password);
            System.out.println("Connect successful!");
            
        } catch (SQLException e) {
            System.out.println("Connect fail!");
            return false;
        }
        return true;
    }

    public boolean getConnection(String _hostname, String _portNumber,
            String _dbname, String _username, String _password) {
        
        //prepare new connection with MSSQL server's driver
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            System.out.println("Class not found!");
            return false;
        }
        
        //get connecttion
        url = "jdbc:sqlserver://" + _hostname;
        if (!_portNumber.equals("")) {
            url += ":" + _portNumber;
        }
        url += ";databaseName=" + _dbname;

        try {
            conn = DriverManager.getConnection(url, _username, _password);
            System.out.println("Connect successful!");
        } catch (SQLException e) {
            System.out.println("Connect fail!");
            return false;
        }
        return true;
    }

    public void Close(){
        try {
            if(conn!=null||!conn.isClosed()){
                conn.close();
                System.out.println("Connection closed!");
            }
        } catch (Exception e) {
            System.out.println("Fail to close connection!");
        }
        
    }
    
    //get resultset
    public ResultSet getResultSet(String query){
        res=null;
        getDefaultConnection();
        try {
             stm=conn.createStatement();
            res=stm.executeQuery(query);
        } catch (Exception e) {
            System.out.println("[DbAccess][getResultSet]"+e.getMessage());
        }
        return res;
    }
    
    
    //getter
    public Connection Connection(){
        return conn;
    }
    
    /**
     * @param hostname the hostname to set
     */
    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    /**
     * @param portNumber the portNumber to set
     */
    public void setPortNumber(String portNumber) {
        this.portNumber = portNumber;
    }

    /**
     * @param dbname the dbname to set
     */
    public void setDbname(String dbname) {
        this.dbname = dbname;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
}
